/*     */ package blockbuster;
/*     */ 
/*     */ import blockbuster.math.MathUtils;
/*     */ import blockbuster.math.Variable;
/*     */ import blockbuster.math.molang.MolangException;
/*     */ import blockbuster.math.molang.MolangParser;
/*     */ import blockbuster.math.molang.expressions.MolangExpression;
/*     */ import blockbuster.utils.Interpolations;
/*     */ import com.google.gson.JsonArray;
/*     */ import com.google.gson.JsonElement;
/*     */ import com.google.gson.JsonObject;
/*     */ 
/*     */ public class BedrockCurve
/*     */ {
/*  15 */   public BedrockCurveType type = BedrockCurveType.LINEAR;
/*  16 */   public MolangExpression[] nodes = new MolangExpression[] { MolangParser.ZERO, MolangParser.ONE, MolangParser.ZERO };
/*     */   
/*     */   public MolangExpression input;
/*     */   public MolangExpression range;
/*     */   public Variable variable;
/*     */   
/*     */   public double compute() {
/*  23 */     return computeCurve(this.input.get() / this.range.get());
/*     */   }
/*     */ 
/*     */   
/*     */   private double computeCurve(double factor) {
/*  28 */     int length = this.nodes.length;
/*     */     
/*  30 */     if (length == 0)
/*     */     {
/*  32 */       return 0.0D;
/*     */     }
/*  34 */     if (length == 1)
/*     */     {
/*  36 */       return this.nodes[0].get();
/*     */     }
/*     */     
/*  39 */     if (factor < 0.0D)
/*     */     {
/*  41 */       factor = -(1.0D + factor);
/*     */     }
/*     */     
/*  44 */     factor = MathUtils.clamp(factor, 0.0D, 1.0D);
/*     */     
/*  46 */     if (this.type == BedrockCurveType.HERMITE) {
/*     */       
/*  48 */       if (length <= 3)
/*     */       {
/*  50 */         return this.nodes[length - 2].get();
/*     */       }
/*     */       
/*  53 */       factor *= (length - 3);
/*  54 */       int i = (int)factor + 1;
/*     */       
/*  56 */       MolangExpression beforeFirst = getNode(i - 1);
/*  57 */       MolangExpression molangExpression1 = getNode(i);
/*  58 */       MolangExpression molangExpression2 = getNode(i + 1);
/*  59 */       MolangExpression afterNext = getNode(i + 2);
/*     */       
/*  61 */       return Interpolations.cubicHermite(beforeFirst.get(), molangExpression1.get(), molangExpression2.get(), afterNext.get(), factor % 1.0D);
/*     */     } 
/*     */     
/*  64 */     factor *= (length - 1);
/*  65 */     int index = (int)factor;
/*     */     
/*  67 */     MolangExpression first = getNode(index);
/*  68 */     MolangExpression next = getNode(index + 1);
/*     */     
/*  70 */     return Interpolations.lerp(first.get(), next.get(), factor % 1.0D);
/*     */   }
/*     */ 
/*     */   
/*     */   private MolangExpression getNode(int index) {
/*  75 */     if (index < 0)
/*     */     {
/*  77 */       return this.nodes[0];
/*     */     }
/*  79 */     if (index >= this.nodes.length)
/*     */     {
/*  81 */       return this.nodes[this.nodes.length - 1];
/*     */     }
/*     */     
/*  84 */     return this.nodes[index];
/*     */   }
/*     */ 
/*     */   
/*     */   public void fromJson(JsonObject object, MolangParser parser) throws MolangException {
/*  89 */     if (object.has("type"))
/*     */     {
/*  91 */       this.type = BedrockCurveType.fromString(object.get("type").getAsString());
/*     */     }
/*     */     
/*  94 */     if (object.has("input"))
/*     */     {
/*  96 */       this.input = parser.parseJson(object.get("input"));
/*     */     }
/*     */     
/*  99 */     if (object.has("horizontal_range"))
/*     */     {
/* 101 */       this.range = parser.parseJson(object.get("horizontal_range"));
/*     */     }
/*     */     
/* 104 */     if (object.has("nodes")) {
/*     */       
/* 106 */       JsonArray nodes = object.getAsJsonArray("nodes");
/* 107 */       MolangExpression[] result = new MolangExpression[nodes.size()];
/*     */       
/* 109 */       for (int i = 0, c = result.length; i < c; i++)
/*     */       {
/* 111 */         result[i] = parser.parseJson(nodes.get(i));
/*     */       }
/*     */       
/* 114 */       this.nodes = result;
/*     */     } 
/*     */   }
/*     */ 
/*     */   
/*     */   public JsonElement toJson() {
/* 120 */     JsonObject curve = new JsonObject();
/* 121 */     JsonArray nodes = new JsonArray();
/*     */     
/* 123 */     curve.addProperty("type", this.type.id);
/* 124 */     curve.add("nodes", (JsonElement)nodes);
/* 125 */     curve.add("input", this.input.toJson());
/* 126 */     curve.add("horizontal_range", this.range.toJson());
/*     */     
/* 128 */     for (MolangExpression expression : this.nodes)
/*     */     {
/* 130 */       nodes.add(expression.toJson());
/*     */     }
/*     */     
/* 133 */     return (JsonElement)curve;
/*     */   }
/*     */ }


/* Location:              D:\fanbian\[D][龙之核心]DragonCore-2.6.0.3.jar!\blockbuster\BedrockCurve.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */